<?php

use console\db\Migration;
use yii\helpers\Console;
use console\models\AdminUser;
use mdm\admin\components\Configs;

/**
 * 初始化创始人数据
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m200104_091351_d_founder extends Migration {

    /**
     * @inheritdoc
     */
    public function safeUp() {
        $founder = Yii::$app->params['founder'];

        $user = new AdminUser();
        $user->setAttributes([
            'username' => $founder['username'],
            'mobile' => $founder['mobile'],
            'realname' => 'Youmbe',
            'password_hash' => Yii::$app->security->generatePasswordHash($founder['password']),
            'auth_key' => Yii::$app->security->generateRandomString(),
            'password_reset_token' => '',
            'created_at' => time(),
            'updated_at' => time(),
        ]);
        if (!$user->save()) {
            Console::output("测试数据初始化出错，" . $user->getErrorMsg() . '（请手动初始化）');
            return false;
        }
    }

    /**
     * @inheritdoc
     */
    public function safeDown() {
        $this->truncateTable(Configs::instance()->userTable);
    }

}
